package com.example.picture;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private CircleImageView image;
    private Button res;
    private Button network;
    private Button res2;
    private Button res1;
    private Button network2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void displayImage(Context context, Object path, ImageView imageView) {
        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .placeholder(R.drawable.bg4)//加载中
                .circleCrop()//圆形图片
                .error(R.drawable.preview)//加载失败
                .fallback(R.drawable.ww);//加载中回调过程中的图片
        Glide.with(context).load(path).into(imageView);
    }
    private void initView() {
        image = (CircleImageView) findViewById(R.id.image);
        res = (Button) findViewById(R.id.res);
        res.setOnClickListener(this);
        network = (Button) findViewById(R.id.network);
        network.setOnClickListener(this);
        res2 = (Button) findViewById(R.id.res2);
        res2.setOnClickListener(this);
        res1 = (Button) findViewById(R.id.res1);
        res1.setOnClickListener(this);
        network2 = (Button) findViewById(R.id.network2);
        network2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.res:
                Glide.with(MainActivity.this).load(R.drawable.ww).into(image);
                break;
            case R.id.network:
                Glide.with(MainActivity.this).load("http://pic.qqtn.com/file/2013/2014-9/2014092716004642145.jpg").into(image);
                break;
            case R.id.res2:
                displayImage(MainActivity.this, R.drawable.ww, image);
                break;
            case R.id.res1:
                image.setImageResource(R.drawable.bg1);
                break;
            case R.id.network2:
                Glide.with(MainActivity.this).load(
                        "https://p4.music.126.net/YlxcLFtQHjgS2VnW9EDPWQ==/7874702278513676.jpg")
                        .into(image);
                break;
        }
    }
}
